package com.lvmama.rhino.web;

import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.base.ExcelUtils;
import com.lvmama.rhino.entity.DeviceNotificationStatisticVO;
import com.lvmama.rhino.enums.CommonStringType;
import com.lvmama.rhino.report.usermode.DocReport;
import com.lvmama.rhino.report.usermode.DocReportFactory;
import com.lvmama.rhino.service.DeviceNotificationStatisticService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author wuchuantong
 * @version V1.0
 * @Title: DeviceNotificationStatisticController
 * @Package com.lvmama.rhino.web
 * @Description: 设备新增统计
 * @date 2017/3/6 14:01
 */
@Controller
@RequestMapping("deviceNotificationStatistic")
public class DeviceNotificationStatisticController {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private DeviceNotificationStatisticService deviceNotificationStatisticService;

    /**
     * 根据查询条件查询表数据，并返回给页面
     *
     * @param statisticVO 封装的分页查询条件
     * @param model       用于放入页面需要的数据
     * @return 需要跳转的路径
     */
    @RequiresPermissions("sys:deviceNotificationStatistic:view")
    @RequestMapping("list")
    public String list(DeviceNotificationStatisticVO statisticVO, Model model) {
        //拆分时间范围
        PageInfo<Map<String, Object>> page = getListMapPage(statisticVO);
        model.addAttribute("page", page);
        model.addAttribute("conditionVO", statisticVO);
        model.addAttribute("statistics", page.getList());
        putPlatformToModel(model);
        return "application/pushStatistic/deviceNotificationStatisticList";
    }

    private PageInfo<Map<String, Object>> getListMapPage(DeviceNotificationStatisticVO statisticVO) {
        if (StringUtils.isNotBlank(statisticVO.getStatisticDateRange()) && statisticVO.getStatisticDateRange().contains("至")) {
            String[] dateRanges = statisticVO.getStatisticDateRange().split("至");
            statisticVO.setStatisticStartDate(dateRanges[0].trim());
            statisticVO.setStatisticEndDate(dateRanges[1].trim());
        } else {
            Date yesterday = DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.DATE), -1);
            String yesterdayStr = DateFormatUtils.format(yesterday, "yyyy-MM-dd HH:mm:ss");
            statisticVO.setStatisticStartDate(yesterdayStr);
            statisticVO.setStatisticEndDate(yesterdayStr);
            statisticVO.setStatisticDateRange(yesterdayStr + " 至 " + yesterdayStr);
        }
        return deviceNotificationStatisticService.getListMapPage(statisticVO);
    }

    /**
     * 跳转至列表页面
     *
     * @param model 用于放入页面需要的数据
     * @return 跳转页面
     */
    @RequiresPermissions("sys:deviceNotificationStatistic:view")
    @RequestMapping("toList")
    public String toList(Model model) {
        putPlatformToModel(model);
        return "application/pushStatistic/deviceNotificationStatisticList";
    }

    /**
     * 导出符合条件的数据
     *
     * @param statisticVO 封装的分页查询
     * @param response    httpresponse
     * @throws Exception
     */
    @RequiresPermissions("sys:deviceNotificationStatistic:export")
    @RequestMapping("export")
    public void export(DeviceNotificationStatisticVO statisticVO, HttpServletResponse response) throws Exception {
        try {
            statisticVO.setPageSize(Integer.MAX_VALUE);
            statisticVO.setOrderBy("t.statistic_date asc,t.platform DESC");
            PageInfo<Map<String, Object>> page = getListMapPage(statisticVO);
            DocReport dr = new DocReportFactory().createReport();
            dr.title("推送开启情况统计")
                    .header("设备平台", "总用户数", "新增用户数", "日活跃用户数", "开启推送用户数", "关闭推送用户数", "新增开启推送数", "新增关闭推送数", "统计日期")
                    .body("platform", "totalNum", "incNum", "activeNum", "allowNum", "prohibitNum", "incAllowNum", "incProhibitNum", "statisticDate")
                    .dataMaps(page.getList());
            ExcelUtils.docReport2Web("推送开启情况统计" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"), dr, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    private void putPlatformToModel(Model model) {
        CommonStringType[] getPlatformType = CommonStringType.getPlatformType();
        //设备平台下拉列表
        List<CommonStringType> platformTypeList = Arrays.asList(getPlatformType);
        model.addAttribute("platformTypeList", platformTypeList);
    }
}
